﻿<%@ Page Title="User Search" Language="C#" MasterPageFile="~/Administration/Site.Master" AutoEventWireup="true" CodeBehind="UserSearch.aspx.cs" Inherits="AdministrationWeb.Administration.WebForm3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="headerText" runat="server">
    User Search
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <table>
        <tr>
            <td>
                First Name:
            </td>
            <td>
                <asp:TextBox ID="txtFirstName" runat="server" Width="300" />
            </td>
        </tr>
        <tr>
            <td>
                Last Name:
            </td>
            <td>
                <asp:TextBox ID="txtLastName" runat="server" Width="300" />
            </td>
        </tr>
        <tr>
            <td>
                Graduation Year:
            </td>
            <td>
                <asp:TextBox ID="txtGradYear" runat="server" Width="300" />
            </td>
        </tr>
        <tr>
            <td>
                Permission:
            </td>
            <td>
                <asp:DropDownList ID="ddlPermission" runat="server" AppendDataBoundItems="True" DataSourceID="SqlDataSource1" DataTextField="permission_text" DataValueField="permission_id">
                    <asp:ListItem Selected="True"></asp:ListItem>
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationConnectionString %>" ProviderName="<%$ ConnectionStrings:ApplicationConnectionString.ProviderName %>"
                    SelectCommand="select permission_id, permission_text from lkup_permission"></asp:SqlDataSource>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" CssClass="button" />
                &nbsp;
                <asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" Text="Cancel" CssClass="button" />
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource2" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BorderColor="#006600"
                    BorderStyle="Double" CellPadding="4" ForeColor="#333333" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" PageIndex="5" Visible="False" 
                    DataKeyNames="user_id">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:CommandField ButtonType="Button" SelectText="Edit" ShowSelectButton="True">
                            <ControlStyle CssClass="button" />
                        </asp:CommandField>
                        <asp:CommandField ButtonType="Button" ShowDeleteButton="True">
                            <ControlStyle CssClass="button" />
                        </asp:CommandField>
                        <asp:BoundField DataField="user_id" HeaderText="User ID" ReadOnly="True" Visible="False" />
                        <asp:BoundField DataField="username" HeaderText="User Name" />
                        <asp:BoundField DataField="first_name" HeaderText="First" />
                        <asp:BoundField DataField="last_name" HeaderText="Last" />
                        <asp:BoundField DataField="permission" HeaderText="Permission" />
                        <asp:BoundField DataField="graduation_year" HeaderText="Grad Year" />
                        <asp:BoundField DataField="email" HeaderText="Email" />
                    </Columns>
                    <EditRowStyle BackColor="#7C6F57" />
                    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#E3EAEB" />
                    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F8FAFA" />
                    <SortedAscendingHeaderStyle BackColor="#246B61" />
                    <SortedDescendingCellStyle BackColor="#D4DFE1" />
                    <SortedDescendingHeaderStyle BackColor="#15524A" />
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationConnectionString %>" ProviderName="<%$ ConnectionStrings:ApplicationConnectionString.ProviderName %>"
                    SelectCommand="select user_id, graduation_year, username, permission, first_name, last_name, email 
                    from users
                    where lcase(first_name) like lcase(concat(?firstname, &quot;%&quot;))
                    and lcase(last_name) like lcase(concat(?lastname, &quot;%&quot;))
                    and lcase(permission) like lcase(concat(?permission, &quot;%&quot;))
                    and (graduation_year between ?minyear and ?maxyear or graduation_year is null or graduation_year = '')
order by permission asc, username asc, graduation_year;" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM users where user_id = ?original_user_id"
                    OldValuesParameterFormatString="original_{0}" UpdateCommand="update users set(username = ?username, graduation_year = ?graduation_year, permission = ?permission, first_name = ?first_name, last_name = ?last_name, email = ?email) where user_id = $user_id">
                    <DeleteParameters>
                        <asp:Parameter Name="original_user_id" />
                    </DeleteParameters>
                    <SelectParameters>
                        <asp:ControlParameter ControlID="txtFirstName" DefaultValue="%" Name="firstname" PropertyName="Text" />
                        <asp:ControlParameter ControlID="txtLastName" DefaultValue="%" Name="lastname" PropertyName="Text" />
                        <asp:ControlParameter ControlID="ddlPermission" DefaultValue="%" Name="permission" PropertyName="SelectedValue" />
                        <asp:ControlParameter ControlID="txtGradYear" DefaultValue="-1" Name="minyear" PropertyName="Text" />
                        <asp:ControlParameter ControlID="txtGradYear" DefaultValue="999999" Name="maxyear" PropertyName="Text" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
        </tr>
    </table>
</asp:Content>
